from django.contrib import admin
from .models import AgentType, Agent, AgentRating, Conversation, Message


@admin.register(AgentType)
class AgentTypeAdmin(admin.ModelAdmin):
    """智能体类型管理"""
    list_display = ('name', 'description', 'icon', 'created_at')
    search_fields = ('name', 'description')
    readonly_fields = ('created_at',)


@admin.register(Agent)
class AgentAdmin(admin.ModelAdmin):
    """智能体管理"""
    list_display = ('name', 'creator', 'agent_type', 'status', 'is_public', 'usage_count', 'rating', 'created_at')
    list_filter = ('status', 'is_public', 'is_featured', 'agent_type', 'created_at')
    search_fields = ('name', 'description', 'creator__username')
    readonly_fields = ('usage_count', 'rating', 'rating_count', 'created_at', 'updated_at', 'last_used_at')
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'description', 'avatar', 'creator', 'agent_type')
        }),
        ('配置', {
            'fields': ('system_prompt', 'personality', 'capabilities', 'knowledge_domains')
        }),
        ('AI模型设置', {
            'fields': ('model_name', 'temperature', 'max_tokens')
        }),
        ('状态设置', {
            'fields': ('status', 'is_public', 'is_featured')
        }),
        ('统计信息', {
            'fields': ('usage_count', 'rating', 'rating_count', 'created_at', 'updated_at', 'last_used_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(AgentRating)
class AgentRatingAdmin(admin.ModelAdmin):
    """智能体评分管理"""
    list_display = ('agent', 'user', 'rating', 'created_at')
    list_filter = ('rating', 'created_at')
    search_fields = ('agent__name', 'user__username', 'comment')
    readonly_fields = ('created_at',)


@admin.register(Conversation)
class ConversationAdmin(admin.ModelAdmin):
    """对话管理"""
    list_display = ('title', 'agent', 'user', 'created_at', 'updated_at')
    list_filter = ('agent', 'created_at')
    search_fields = ('title', 'agent__name', 'user__username')
    readonly_fields = ('created_at', 'updated_at')


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    """消息管理"""
    list_display = ('conversation', 'role', 'content_preview', 'created_at')
    list_filter = ('role', 'created_at')
    search_fields = ('conversation__title', 'content')
    readonly_fields = ('created_at',)
    
    def content_preview(self, obj):
        return obj.content[:100] + '...' if len(obj.content) > 100 else obj.content
    content_preview.short_description = '内容预览'